package com.jixiang.goods.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.jixiang.goods.service.GsGoodsInfoService;
import com.jixiang.goods.service.GsGoodsSkusService;
import com.jixiang.model.dictionary.entity.DsRegion;
import com.jixiang.model.goods.dtos.CartDtos;
import com.jixiang.model.goods.entity.GsGoodsInfo;
import com.jixiang.model.goods.entity.GsGoodsSkus;
import com.jixiang.model.order.dtos.OrderDtos;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

import static com.jixiang.common.constants.RedisConstants.REDIS_GOODS_ID;
import static com.jixiang.common.constants.RedisConstants.REDIS_SKU_ID;

@Controller
public class GoodsController {




    @Resource
    private GsGoodsInfoService gsGoodsInfoService;
    @Resource
    private GsGoodsSkusService gsGoodsSkusService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping("goodsApi/goodsInventory")
    public String goodsInventory(@SpringQueryMap String orderDtos){
//    public String goodsInventory(@RequestBody(required = false) List<CartDtos> cartDtosList){
        List<GsGoodsInfo> list = gsGoodsInfoService.query().list();
        list.forEach(item -> {
            if(item.getDynamicParamState() == 0){
                stringRedisTemplate.opsForValue().set(REDIS_GOODS_ID + item.getId() + REDIS_SKU_ID + 0,item.getInventory().toString());
            }else {
                List<GsGoodsSkus> gsGoodsSkusList = gsGoodsSkusService.query().eq("goods_id", item.getId()).list();
                if(gsGoodsSkusList!=null || !gsGoodsSkusList.isEmpty()){
                    gsGoodsSkusList.forEach(items -> {
                        stringRedisTemplate.opsForValue().set(REDIS_GOODS_ID + item.getId() + REDIS_SKU_ID + items.getId(),items.getInventory().toString());
                    });
                }
            }
        });
        return "1";
    }

}
